30.01.2026
Website speed is a critical ranking factor and a major part of user experience. A slow website increases bounce rates, hurts SEO, and reduces conversions. One of the most effective ways to improve page speed on an Apache server is by optimizing your .htaccess file.
The .htaccess file allows you to control caching, compression, browser behavior, and server performance—without touching your core website code. When configured correctly, it can significantly boost your PageSpeed score.
What Is .htaccess and Why It Matters for Speed
.htaccess (Hypertext Access) is a configuration file used by Apache servers. It lets you apply performance rules at the server level, such as:
- Enabling browser caching
- Compressing files
- Reducing server requests
- Controlling headers
- Improving Core Web Vitals
1. Enable Browser Caching (Very Important)
Browser caching allows returning visitors to load your website faster by storing static files locally.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
Benefits:
- Fixes “Serve static assets with an efficient cache policy”
- Faster repeat visits
- Improved Google PageSpeed score
2. Enable GZIP Compression
Compression reduces file size before sending it to the browser.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css text/javascript
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
Benefits:
- Reduces page size by up to 70%
- Faster loading on slow connections
- Improves Core Web Vitals (LCP, FCP)
3. Enable Keep-Alive Connections
Keep-Alive allows multiple files to be sent over a single connection.
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
Benefits:
- Fewer server connections
- Faster asset delivery
- Lower server load
4. Reduce Server Response Time (TTFB)
Improve Time to First Byte by combining caching and headers.
<IfModule mod_headers.c>
Header set Cache-Control "public, max-age=31536000"
</IfModule>
Disable ETags (Optional but Helpful)
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
Benefits:
- Prevents cache conflicts
- Improves CDN compatibility
5. Force HTTPS (Speed + Security)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Benefits:
- Enables HTTP/2 for faster loading
- Better SEO performance
- Improved user trust
6. Prevent Hotlinking (Optional)
Hotlinking wastes bandwidth and slows your server.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|webp)$ - [F]
Common Mistakes to Avoid
- ❌ Over-caching HTML files
- ❌ Not using cache versioning (style.css?v=2)
- ❌ Adding conflicting rules
- ❌ Forgetting to clear cache after changes
How to Test Performance After Changes
- Clear browser cache
- Clear server or plugin cache
- Test using Google PageSpeed Insights, GTmetrix, or WebPageTest
You should see improvements in load time, Core Web Vitals, and PageSpeed score.
Final Thoughts
Optimizing your .htaccess file is one of the fastest and most effective ways to improve website speed on Apache servers. When done correctly, it reduces load time, improves SEO, and delivers a better user experience.

